<template>
    <div class="menu-wrapper">
        <template v-if="hasOneShowingChild(item.list,item)">
            <router-link :to="resolvePath(onlyOneChild.url)">
                <el-menu-item :index="onlyOneChild.url" @click="save(onlyOneChild.menuId)" :class="{'submenu-title-noDropdown' : isNest }">
                    <item :icon="item.icon" :title="onlyOneChild.name"/>
                </el-menu-item>
            </router-link>
        </template>
        <el-submenu v-else ref="subMenu" :index="item.url" popper-append-to-body @click="save(item.menuId)">
            <template v-slot:title>
                <item v-if="item.list" :icon="item.icon" :title="item.name" />
            </template>
            <sidebar-item
                v-for="child in item.list"
                :key="child.menuId"
                :is-nest='true'
                :item='child'
                :base-path="resolvePath(child.url)"
                class="nest-menu"
                />
        </el-submenu>
    </div>
</template>

<script>
import path from 'path'
import Item from './Item'

export default {
    name: "SidebarItem",
    components: { Item },
    props: {
        item: {
            type: Object,
            required: true
        },
        isNest: {
            type: Boolean,
            required: false
        },
        basePath: {
            type: String,
            default: ''
        }
    },
    data(){
        this.onlyOneChild = null
        return {
            
        }
    },
	created(){
	},
    mounted(){
        // console.log(this.item)
		
    },
    methods:{
		//存储menuId
		save(menuId){
			// sessionStorage.setItem('menuId',menuId);
		},
        hasOneShowingChild(children = [],parent){
			if(children==""){
				children=[]
			}
            const showingChildren = children.filter(item =>{
                this.onlyOneChild = item
                return true
            })
           
            // 当只有一个子路由,子路由默认展示
            if(showingChildren.length === 1){
                return true
            }
            // 没有子路由则显示父路由
            if(showingChildren.length === 0){
                this.onlyOneChild = {...parent, path: '', noShowingChildren: true }
                return true
            }
            //  console.log(this.onlyOneChild)
            return false
        },
        resolvePath(routePath){
            return path.resolve(routePath)
        }
    }    
}
</script>

<style>
.el-menu-item.is-active {
    background: #1A86EE !important;
}
/* .el-submenu .el-menu-item{
    height: 40px !important;
    line-height: 40px !important;
    margin: 2px 0 3px !important;
}
.el-menu-item{
    height: 40px !important;
    line-height: 40px !important;
    margin: 2px 0 3px !important;
}
.el-submenu{
    height: 40px !important;
    line-height: 40px !important;
    margin: 2px 0 3px !important;
}
.el-submenu__title{
    height: 40px !important;
    line-height: 40px !important;
    margin: 2px 0 3px !important;
} */
</style>